package com.engine.jucailinkq.attendance.attendanceanalysis.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.jucailinkq.attendance.attendanceanalysis.service.UtilService;
import com.engine.jucailinkq.attendance.attendanceanalysis.service.impl.UtilServiceImpl;
import com.engine.jucailinkq.attendance.attendanceanalysis.thread.AttendanceAnalysisSingleThread;
import com.engine.jucailinkq.attendance.attendanceanalysis.thread.AttendanceAnalysisThread;
import com.engine.jucailinkq.attendance.attendanceanalysis.wrapper.AttendanceAnalysisWrapper;
import com.engine.jucailinkq.attendance.attendanceplan.service.AttendancePlanService;
import com.engine.jucailinkq.attendance.attendanceplan.service.impl.AttendancePlanServiceImpl;
import com.engine.jucailinkq.attendance.workflow.service.AllowanceService;
import com.engine.jucailinkq.attendance.workflow.service.impl.AllowanceServiceImpl;
import com.engine.common.util.*;
import com.engine.jucailinkq.common.util.ApiReturnTools;
import com.engine.jucailinkq.common.util.CommonUtil;
import com.engine.jucailinkq.common.util.DateUtil;
import com.engine.jucailinkq.common.util.DbTools;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.ThreadPoolUtil;
import weaver.general.Util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import java.time.ZoneOffset;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

@Slf4j
public class AttendanceanalysisAction {
    private AttendancePlanService basicsetService = ServiceUtil.getService(AttendancePlanServiceImpl.class);
    private UtilService utilService = ServiceUtil.getService(UtilServiceImpl.class);
    private AttendanceAnalysisWrapper attendanceAnalysisService = ServiceUtil.getService(AttendanceAnalysisWrapper.class);
    private AllowanceService allowanceService = ServiceUtil.getService(AllowanceServiceImpl.class);

    /**
     * 排班分析
     * @return
     */
    @GET
    @Path("/attendanceAnalysisByHrmAndDate")
    @Produces({"text/plain"})
    public String getSchedulingData(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            BaseBean baseBean = new BaseBean();
            log.info("********AttendanceanalysisAction start********");
            Map<String,Object> paramMap = ParamUtil.request2Map(request);
            /*Map<String,Object> paramMap = Maps.newHashMap();
            paramMap.put("startDate","2024-08-21");
            paramMap.put("endDate","2024-08-21");
            paramMap.put("userIds","89");*/

            RecordSet rs = new RecordSet();
            String startDate = Util.null2String(paramMap.get("startDate"));
            String startBeforeDate = DateUtil.beforeDay(startDate,2);
            String endDate = Util.null2String(paramMap.get("endDate"));
            String endAfterDate = DateUtil.AfterDay(endDate,2);
            String userIds = Util.null2String(paramMap.get("userIds"));
            int betweenDays = DateUtil.getBetWeenDays(startDate,endDate);
            String queryDepartEmployeeSql = "select resourceid id,changedate name from HRMSTATUSHISTORY  where type_n = 5";
            //获取离职日期
            Map<String,Object> departEmployeeMap = DbTools.getSqlToMapList(queryDepartEmployeeSql);

            log.info("initExtensionClassHolder start.");
            //初始化扩展类
            CommonUtil.initExtensionClassHolder();
            log.info("initExtensionClassHolder end.");

            int availableProcessors = Runtime.getRuntime().availableProcessors();
            baseBean.writeLog("AttendanceanalysisAction availableProcessors:" + availableProcessors);

            if ("".equals(userIds)){
                String queryUserSql = "select id,companystartdate from hrmresource where status <> '5' and status <> '4' and status <> '7'";
                List<Map<String,Object>> userList = DbTools.getSqlToList(queryUserSql);
                String sql = "select id,userid,signdate,signtime from hrmschedulesign where isincom=1 and signdate >= ? and signdate <= ? order by signdate,signtime";
                List<Map<String,Object>> dataList = DbTools.getSqlToList(sql,startBeforeDate,endAfterDate);
                //补打卡记录
                sql = "select id,bdkry userid,dkrq signdate,dksj signtime from uf_jcl_kq_bdkjl where dkrq >= ? and dkrq <= ? and jlzt=1 order by dkrq,dksj";
                List<Map<String,Object>> makeUpList = DbTools.getSqlToList(sql,startBeforeDate,endAfterDate);
                if (makeUpList.size() > 0){
                    dataList.addAll(makeUpList);
                }
                dataList = dataList.stream().sorted(Comparator.comparing(e->DateUtil.getTime(e.get("signdate")+" "+e.get("signtime")).toInstant(ZoneOffset.of("+8")).toEpochMilli())).collect(Collectors.toList());

                Map<String, List<Map<String,Object>>> collect = dataList.stream().collect(Collectors.groupingBy(e -> Util.null2String(e.get("userid"))));
                sql = "select sjzt,sgsj,ygid,rq from uf_jcl_kq_cqjg where rq>=? and rq<=?";
                List<Map<String,Object>> attendaceResult = DbTools.getSqlToList(sql,startDate,endDate);
                Map<String,List<Map<String,Object>>> attendaceGroupById = attendaceResult.stream().collect(Collectors.groupingBy(e->e.get("ygid").toString()));

//                ExecutorService executorService = Executors.newFixedThreadPool(5);
                for (Map<String,Object> userMap :userList){
//                    try {
//                        executorService.execute(new AttendanceAnalysisThread(userMap, departEmployeeMap, startBeforeDate, endAfterDate, collect, attendaceGroupById, betweenDays, startDate, paramMap));
//                    } catch (Exception e) {
//                        baseBean.writeLog("getSchedulingData batch thread error:" + JSONObject.toJSONString(userMap) + "、" + e);
//                    }

                    String userId = userMap.get("id").toString();
                    //入职日期
                    String companystartDate = Util.null2String(userMap.get("companystartdate"));
                    //离职日期
                    String terminationDate = Util.null2String(departEmployeeMap.get(userId));

                    log.info("***********analysis userId:{} start***********", userId);
                    //出勤结果，进出时间
                    Map<String, Map<String, Object>> clockInTimeMap = Maps.newHashMap();
                    /**获得人员考勤项目*/
                    Map<String, Object> param = Maps.newHashMap();
                    param.put("startDate", startBeforeDate);
                    param.put("endDate", endAfterDate);
                    param.put("resourceId", userId);
                    Map<String, Object> dataMap = basicsetService.getAttendanceItemsByPerson(param);
                    List<Map<String, Object>> attendanceItems = (List<Map<String, Object>>) dataMap.get("data");

                    /**
                     *获得人员综合工时
                     */
                    Map<String, Object> workHourdataMap = basicsetService.getWorkHoursItemByPerson(param);
                    List<Map<String, Object>> workHourItems = (List<Map<String, Object>>) workHourdataMap.get("data");

                    /** 获取人员班次*/
                    Map<String, Object> classesParamMap = Maps.newHashMap();
                    classesParamMap.put("tableName", "uf_pbjg");
                    classesParamMap.put("startDate", startBeforeDate);
                    classesParamMap.put("endDate", endAfterDate);
                    classesParamMap.put("pblx", "0");
                    classesParamMap.put("pbdx", userId);
                    classesParamMap.put("current", "1");
                    classesParamMap.put("pageSize", 9999);
                    classesParamMap.put("recurrence", 1);
                    classesParamMap.put("clockInTimeMap", clockInTimeMap);
                    classesParamMap.put("clockInData", collect.get(userId));
                    classesParamMap.put("attendanceItems", attendanceItems);
                    Map<String, Object> schedulMap = utilService.getSchedulingInFormation(classesParamMap);


                    Map<String, List<Map<String, Object>>> attendaceResultMap = Maps.newHashMap();
                    if (attendaceGroupById.get(userId) != null && attendaceGroupById.get(userId).size() > 0) {
                        attendaceResultMap = attendaceGroupById.get(userId).stream().collect(Collectors.groupingBy(e -> e.get("rq").toString()));
                    }

                    for (int i = 0; i <= betweenDays; i++) {
                        String date = DateUtil.AfterDay(startDate, i);
                        if (("".equals(companystartDate) || DateUtil.getTime(date).compareTo(DateUtil.getTime(companystartDate)) > 0)
                                && ("".equals(terminationDate) || DateUtil.getTime(date).compareTo(DateUtil.getTime(terminationDate)) < 0)) {
                            try {
                                attendanceAnalysisService.attendanceAnalysisForApi(userId, date, collect.get(userId), attendanceItems, schedulMap, workHourItems, clockInTimeMap, attendaceResultMap.get(date));
                            } catch (Exception e) {
                                baseBean.writeLog("attendanceAnalysisForApi A param:" + userId + "、" + date + "; error:" + e.getMessage());
                            }
                        }

                        // 入职日期及之前、离职日期及之后的数据逻辑删除
                        if ((StringUtils.isNotEmpty(companystartDate) && DateUtil.getTime(date).compareTo(DateUtil.getTime(companystartDate)) <= 0)
                                || (StringUtils.isNotEmpty(terminationDate) && DateUtil.getTime(date).compareTo(DateUtil.getTime(terminationDate)) >= 0)) {
                            rs.executeUpdate("UPDATE uf_jcl_kq_cqjg SET sfsc = 1 WHERE ygid = ? AND rq = ?", userId, date);
                        }
                    }
                    allowanceService.addAllowanceRecords(paramMap);
                }
            }else {
                String sql = "select id,userid,signdate,signtime from hrmschedulesign where isincom=1 and userid in ("+userIds+") and  signdate >= ? and signdate <= ? order by signdate,signtime";
                List<Map<String,Object>> dataList = DbTools.getSqlToList(sql,startBeforeDate,endAfterDate);

                String queryUserSql = "select id,companystartdate from hrmresource where id in ("+userIds+")";
                List<Map<String,Object>> userList = DbTools.getSqlToList(queryUserSql);
                Map<String,List<Map<String,Object>>> userGroupMap = userList.stream().collect(Collectors.groupingBy(e->e.get("id").toString()));

                //补打卡记录
                sql = "select id,bdkry userid,dkrq signdate,dksj signtime from uf_jcl_kq_bdkjl where bdkry in ("+userIds+") and dkrq >= ? and dkrq <= ? and jlzt=1 order by dkrq,dksj";
                List<Map<String,Object>> makeUpList = DbTools.getSqlToList(sql,startBeforeDate,endAfterDate);
                if (makeUpList.size() > 0){
                    dataList.addAll(makeUpList);
                }
                baseBean.writeLog("getSchedulingData:" + userIds + "、" + JSONObject.toJSONString(dataList));
                dataList = dataList.stream().sorted(Comparator.comparing(e->DateUtil.getTime(e.get("signdate")+" "+e.get("signtime")).toInstant(ZoneOffset.of("+8")).toEpochMilli())).collect(Collectors.toList());


                Map<String, List<Map<String,Object>>> collect = dataList.stream().collect(Collectors.groupingBy(e -> Util.null2String(e.get("userid"))));

                baseBean.writeLog("getSchedulingData collect:" + JSONObject.toJSONString(collect));
                sql = "select sjzt,sgsj,ygid,rq from uf_jcl_kq_cqjg where rq>=? and rq<=? and ygid in ("+userIds+")";
                List<Map<String,Object>> attendaceResult = DbTools.getSqlToList(sql,startDate,endDate);
                Map<String,List<Map<String,Object>>> attendaceGroupById = attendaceResult.stream().collect(Collectors.groupingBy(e->e.get("ygid").toString()));

//                ExecutorService executorService = ThreadPoolUtil.getThreadPool("AttendanceanalysisActionSingle", "10");
                for (String userId : userIds.split(",")){
//                    try {
//                        executorService.execute(new AttendanceAnalysisSingleThread(userId, departEmployeeMap, startBeforeDate, endAfterDate, collect, attendaceGroupById, betweenDays, startDate, userGroupMap));
//                    } catch (Exception e) {
//                        baseBean.writeLog("getSchedulingData single thread error:" + userId + "、" + e);
//                    }
                    // 获得人员考勤项目
                    Map<String,Object> param = Maps.newHashMap();
                    //入职日期
                    String companystartDate = Util.null2String(userGroupMap.get(userId).get(0).get("companystartdate"));
                    //离职日期
                    String terminationDate = Util.null2String(departEmployeeMap.get(userId));

                    //出勤结果，进出时间
                    Map<String,Map<String,Object>> clockInTimeMap = Maps.newHashMap();
                    param.put("startDate",startBeforeDate);
                    param.put("endDate",endAfterDate);
                    param.put("resourceId",userId);
                    Map<String,Object> dataMap = basicsetService.getAttendanceItemsByPerson(param);
                    List<Map<String,Object>> attendanceItems = (List<Map<String,Object>>)dataMap.get("data");

                    // 获得人员综合工时
                    Map<String,Object> workHourdataMap = basicsetService.getWorkHoursItemByPerson(param);
                    List<Map<String,Object>> workHourItems = (List<Map<String,Object>>)workHourdataMap.get("data");

                    // 获取人员班次
                    Map<String,Object> classesParamMap = Maps.newHashMap();
                    classesParamMap.put("tableName","uf_pbjg");
                    classesParamMap.put("startDate",startBeforeDate);
                    classesParamMap.put("endDate",endAfterDate);
                    classesParamMap.put("pblx","0");
                    classesParamMap.put("pbdx",userId);
                    classesParamMap.put("current","1");
                    classesParamMap.put("pageSize",9999);
                    classesParamMap.put("recurrence",1);
                    classesParamMap.put("clockInTimeMap",clockInTimeMap);
                    classesParamMap.put("clockInData",collect.get(userId));
                    classesParamMap.put("attendanceItems",attendanceItems);
                    baseBean.writeLog("getSchedulingData classesParamMap:" + JSONObject.toJSONString(classesParamMap));
                    Map<String,Object> schedulMap = utilService.getSchedulingInFormation(classesParamMap);
                    baseBean.writeLog("getSchedulingData schedulMap:" + JSONObject.toJSONString(schedulMap));

                    Map<String, List<Map<String, Object>>> attendaceResultMap = Maps.newHashMap();
                    if (attendaceGroupById.get(userId) != null && attendaceGroupById.get(userId).size() > 0) {
                        attendaceResultMap = attendaceGroupById.get(userId).stream().collect(Collectors.groupingBy(e -> e.get("rq").toString()));
                    }
                    baseBean.writeLog("getSchedulingData attendaceResultMap:" + JSONObject.toJSONString(attendaceResultMap));
                    for (int i=0; i<=betweenDays;i++){
                        String date = DateUtil.AfterDay(startDate,i);
                        if (("".equals(companystartDate) || DateUtil.getTime(date).compareTo(DateUtil.getTime(companystartDate)) > 0)
                                && ("".equals(terminationDate) || DateUtil.getTime(date).compareTo(DateUtil.getTime(terminationDate)) < 0)) {
                            try {
                                attendanceAnalysisService.attendanceAnalysisForApi(userId, date, collect.get(userId), attendanceItems, schedulMap, workHourItems, clockInTimeMap, attendaceResultMap.get(date));
                            } catch (Exception e) {
                                baseBean.writeLog("attendanceAnalysisForApi B param:" + userId + "、" + date + "; error:" + e.getMessage());
                            }
                        }

                        // 入职日期及之前、离职日期及之后的数据逻辑删除
                        if ((StringUtils.isNotEmpty(companystartDate) && DateUtil.getTime(date).compareTo(DateUtil.getTime(companystartDate)) <= 0)
                                || (StringUtils.isNotEmpty(terminationDate) && DateUtil.getTime(date).compareTo(DateUtil.getTime(terminationDate)) >= 0)) {
                        }
                    }
                }
                paramMap.put("empIds",paramMap.get("userIds"));
                allowanceService.addAllowanceRecords(paramMap);
            }
            baseBean.writeLog("********AttendanceanalysisAction end********");
            return null;
        }catch (Exception e){
            log.error("execute fail,catch error: [{}]",e);
            return ApiReturnTools.error("500",e.getMessage());
        }
    }

    /**
     * 获得分析数据
     * @return
     */
    @POST
    @Path("/getAnalysisDate")
    @Produces({"text/plain"})
    public String getAnalysisDate(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            Map<String,Object> paramMap = ParamUtil.request2Map(request);
            String startDate = Util.null2String(paramMap.get("startDate"));

            String endDate = Util.null2String(paramMap.get("endDate"));

            String userIds = Util.null2String(paramMap.get("userIds"));
            String sql = "select id,userid,signdate,signtime from hrmschedulesign where isincom=1 and userid in ("+userIds+") and  signdate >= ? and signdate <= ? order by signdate,signtime";
            List<Map<String,Object>> dataList = DbTools.getSqlToList(sql,DateUtil.beforeDay(startDate,1),DateUtil.AfterDay(endDate,1));
            //补打卡记录
            sql = "select id,bdkry userid,dkrq signdate,dksj signtime, lcid, bdklx from uf_jcl_kq_bdkjl where bdkry in ("+userIds+") and dkrq >= ? and dkrq <= ? and jlzt=1 order by dkrq,dksj";
            List<Map<String,Object>> makeUpList = DbTools.getSqlToList(sql,DateUtil.beforeDay(startDate,1),DateUtil.AfterDay(endDate,1));

            //考勤项目
            sql = "select id,mc from uf_jcl_kq_kqxm";
            List<Map<String,Object>> items = DbTools.getSqlToList(sql);
            Map<String,String> itemMap = items.stream().collect(Collectors.toMap(e->Util.null2String(e.get("id")),e->Util.null2String(e.get("mc"))));

            if (makeUpList.size() > 0){
                for (Map<String, Object> makeUpItem : makeUpList) {
                    makeUpItem.put("sjly", "2");
                    makeUpItem.put("bdklxName", itemMap.get(Util.null2String(makeUpItem.get("bdklx"))));
                }
                dataList.addAll(makeUpList);
            }
            Map<String, List<Map<String,Object>>> collect = dataList.stream().collect(Collectors.groupingBy(e -> Util.null2String(e.get("userid"))));

            //请假记录
            sql = "select b.qjry,b.jqlx,b.ksrq,b.kssj,b.jsrq,b.jssj,b.qjsc,b.qtj,b.btj from uf_jcl_kq_qjjl a left join uf_jcl_kq_qjjl_dt1 b on a.id = b.mainid where b.qjry in ("+userIds+") and b.ksrq<=? and b.jsrq>=? and (b.cxqj=0 or b.cxqj is null) and a.jlzt=1";
            List<Map<String,Object>> askforLeaveList = DbTools.getSqlToList(sql,DateUtil.AfterDay(endDate,1), DateUtil.beforeDay(startDate,1));
            for (Map<String,Object> map :askforLeaveList){
                map.put("jqlx_name",itemMap.get(map.get("jqlx")));
            }
            Map<String, List<Map<String,Object>>> askforLeavecollect = askforLeaveList.stream().collect(Collectors.groupingBy(e -> Util.null2String(e.get("qjry"))));

            //出差记录
            sql = "select b.ccr,b.cclx,b.ksrq,b.jsrq,b.kssj,b.jssj,b.ccsc,b.qtcc,b.btcc,b.cxcc from uf_jcl_kq_ccjl a left join uf_jcl_kq_ccjl_dt1 b on a.id = b.mainid where b.ccr in ("+userIds+") and b.ksrq<=? and b.jsrq>=? and (b.cxcc=0 or b.cxcc is null) and a.jlzt=1";
            List<Map<String,Object>> evectionList = DbTools.getSqlToList(sql,DateUtil.AfterDay(endDate,1), DateUtil.beforeDay(startDate,1));
            for (Map<String,Object> map :evectionList){
                map.put("cclx_name",itemMap.get(map.get("cclx")));
            }
            Map<String, List<Map<String,Object>>> evectioncollect = evectionList.stream().collect(Collectors.groupingBy(e -> Util.null2String(e.get("ccr"))));

            //加班计划
            sql = "select b.jbry,b.ksrq,b.kssj,b.jblx,b.jsrq,b.jssj,b.jbsc,b.gsrq from uf_jcl_kq_jbjh a left join uf_jcl_kq_jbjh_dt1 b on a.id=b.mainid where b.jbry in ("+userIds+") and b.gsrq>=? and b.gsrq<=? and (b.jbcx=0 or b.jbcx is null) and a.jlzt=1 order by b.ksrq,b.kssj";
            List<Map<String,Object>> overPlanDataList = DbTools.getSqlToList(sql,startDate,endDate);
            for (Map<String,Object> map :overPlanDataList){
                map.put("jblx_name",itemMap.get(map.get("jblx")));
            }
            Map<String, List<Map<String,Object>>> overPlancollect = overPlanDataList.stream().collect(Collectors.groupingBy(e -> Util.null2String(e.get("jbry"))));

            //加班结果
            sql = "select szjg,jbry,sjksrq,jbjgly,sjjsrq,sjkssj,sjjssj,sjjbsc,jblx,zt from uf_jcl_kq_jbjg where jbry in ("+userIds+") and sjksrq>=? and sjjsrq<=? and zt=1 order by sjksrq,sjkssj";
            List<Map<String,Object>> overPlanResultList = DbTools.getSqlToList(sql,startDate,endDate);
            for (Map<String,Object> map :overPlanResultList){
                map.put("jblx_name",itemMap.get(map.get("jblx")));
            }
            Map<String, List<Map<String,Object>>> overResultcollect = overPlanResultList.stream().collect(Collectors.groupingBy(e -> Util.null2String(e.get("jbry"))));




            List<Map<String,Object>> resultList = Lists.newArrayList();
            for (String userId : userIds.split(",")){
                Map<String,Object> map = Maps.newHashMap();
                Map<String,List<Map<String,Object>>> map1 = Maps.newHashMap();
                map1.put("signDate",collect.get(userId));
                map1.put("askforLeave",askforLeavecollect.get(userId));
                map1.put("evection",evectioncollect.get(userId));
                map1.put("overPlan",overPlancollect.get(userId));
                map1.put("overResult",overResultcollect.get(userId));
                map.put("userId",userId);
                map.put("data",map1);
                resultList.add(map);
            }


            return ApiReturnTools.success(resultList);
        }catch (Exception e){
            log.error("execute fail,catch error: [{}]",e);
            return ApiReturnTools.error("500","saveApplicableOrganization error");
        }
    }

    /**
     * 获得已经设置为默认日历的日历名称信息
     */
    @POST
    @Path("/getDefaultCalendar")
    @Produces({"text/plain"})
    public String getDefaultCalendar(@Context HttpServletRequest request, @Context HttpServletResponse response){
        try {
            Map<String,Object> paramMap = ParamUtil.request2Map(request);
            String sql = "select id, szjg, rlmc, rlsm from uf_jcl_kq_rlmc where mrrl = '1' and szjg=?";
            List<Map<String,Object>> calendarList = DbTools.getSqlToList(sql,paramMap.get("szjg"));
            return ApiReturnTools.success(calendarList);
        }catch (Exception e){
            log.error("execute fail,catch error: [{}]",e);
            return ApiReturnTools.error("500","saveApplicableOrganization error");
        }
    }

    /**
     * 将所有的企业日历取消默认值标记
     */
    @POST
    @Path("/dumpDefaultCalendar")
    @Produces({"text/plain"})
    public String dumpDefaultCalendar(@Context HttpServletRequest request, @Context HttpServletResponse response){
        try {
            Map<String,Object> paramMap = ParamUtil.request2Map(request);
            String sql = "update uf_jcl_kq_rlmc set mrrl = '0' where szjg=?";
            List<Map<String,Object>> resultList = Lists.newArrayList();
            Map<String,Object> map = Maps.newHashMap();
            if(DbTools.update(sql,paramMap.get("szjg")))
                map.put("update","1");
            else
                map.put("update","0");
            resultList.add(map);
            return ApiReturnTools.success(resultList);
        }catch (Exception e){
            log.error("execute fail,catch error: [{}]",e);
            return ApiReturnTools.error("500","saveApplicableOrganization error");
        }
    }

    /**
     * 获得当前日历被引用的次数
     */
    @POST
    @Path("/getCalendarRef")
    @Produces({"text/plain"})
    public String getCalendarRef(@Context HttpServletRequest request, @Context HttpServletResponse response){
        try {
            Map<String,Object> paramMap = ParamUtil.request2Map(request);
            String calendarId = Util.null2String(paramMap.get("id"));
            String sql = "select count(id) refcount from uf_jcl_kq_glpb where qyrl = "+calendarId;
            List<Map<String,Object>> calendarList = DbTools.getSqlToList(sql);
            return ApiReturnTools.success(calendarList);
        }catch (Exception e){
            log.error("execute fail,catch error: [{}]",e);
            return ApiReturnTools.error("500","saveApplicableOrganization error");
        }
    }
}
